Dynomotion

Group: DynoMotion Message: 11628 From: Hardy Family Date: 6/2/2015
Subject: Writing files from KFlop
A few questions about fopen() etc. on the kflop:

1. Where does a file go on the PC if you don't specify an absolute path?

2. The following code doesn't work as expected:
        char filename[100];
        int i;
        double * h;
        FILE * f;

        sprintf(filename, "c:\\temp\\settings-%06d.var", 123456);
        f = fopen(filename, "wt");
        if (f) {
            h = supe->get_setting_ref(BASE_HASHVAR);
            for (i = 0; i < NUM_HASHVARS; ++i, ++h) {
                fprintf(f, "%d\t%f\n", i+BASE_HASHVAR, *h);
            }
            fclose(f);
            printf("Wrote %d settings to %s on PC\n", NUM_HASHVARS, filename);
        }
        else
            printf("Could not open %s on PC\n", filename);

Basically what I am doing is writing a file in NGC ".var" format, based on some configuration data on the kflop.

What is strange is that 'f' always seems to be NULL after fopen(), even though it does create a file on the PC.  If I just ignore the fact that it is NULL and go ahead and use it, then it seems to write data.

On the PC, I can't open the file because Windows is saying that it is in use by another application.  So it looks like fclose() is not getting through to the KMotion server to tell it to close the file.

So is the value of the FILE pointer on the kflop meaningful?

3. What happens if a thread forgets to fclose()?

4. When trying this out, I first used a relative path name, but then fopen() returned NULL, so I thought it wasn't supported.  I then changed the path name to absolute, and again got the NULL.  But this time I found the file in c:\temp, but it was empty.  So finally I got rid of the NULL test.  It wrote 5k data to the file, but now I cannot open thanks to Windows thinking it's in use.

What have I done wrong?

Regards,
SJH


Group: DynoMotion Message: 11631 From: Tom Kerekes Date: 6/3/2015
Subject: Re: Writing files from KFlop
Hi SJH,

Not specifying a path would go to the current directory which is probably unpredictable.

The fopen command will always return NULL and just sends the file name to open to the PC (KMotionServer.exe) without confirmation.   KFLOP only supports one open file at a time.

fclose() should work properly.   

I did notice that if you fail to close the file and try to open a file then it will then be impossible to close the original file.  We will make a change so that if a new file is opened before the previous is closed that the previous will be automatically closed.

If you are writing code on the PC side there are probably better ways to create files from data within KFLOP.  Have KFLOP place data into the gather buffer and upload it to the PC and let the PC format it and write it to the disk.

Regards
TK

Group: DynoMotion Message: 11632 From: Hardy Family Date: 6/3/2015
Subject: Re: Writing files from KFlop
That would explain it, then.  Initially when it saw NULL, it would not close the file (I was thinking Posix - I dunno, it just feels wrong not to check for NULL :-).  That's why it didn't close on the PC.

At present, it's easier to format the file from the kflop side.  When we seriously get into writing a PC app, then we'll probably revisit this.

Regards,
SJH


On Wed, Jun 3, 2015 at 11:14 AM, Tom Kerekes tk@... [DynoMotion] <DynoMotion@yahoogroups.com> wrote:
 

Hi SJH,

Not specifying a path would go to the current directory which is probably unpredictable.

The fopen command will always return NULL and just sends the file name to open to the PC (KMotionServer.exe) without confirmation.   KFLOP only supports one open file at a time.

fclose() should work properly.   

I did notice that if you fail to close the file and try to open a file then it will then be impossible to close the original file.  We will make a change so that if a new file is opened before the previous is closed that the previous will be automatically closed.

If you are writing code on the PC side there are probably better ways to create files from data within KFLOP.  Have KFLOP place data into the gather buffer and upload it to the PC and let the PC format it and write it to the disk.

Regards
TK